package com.rzico.basics.controller.admin;

import com.rzico.annotation.Log;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.RecommendBasicMapper;
import com.rzico.basics.model.RecommendTagVo;
import com.rzico.basics.model.RecommendVo;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.RecommendTagService;
import com.rzico.basics.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "推荐标签")
@RestController
@RequestMapping("/admin/recommendTag")
public class RecommendTagAdminController {

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private RecommendTagService recommendTagService;

    @Autowired
    private RecommendBasicMapper recommendBasicMapper;

    @Autowired
    private TagService tagService;


    @ApiOperation("单个关联推荐")
    @PostMapping("/save")
    public CommResult<RecommendVo> correlationRecommend(@RequestBody Recommend recommend, Long tagId) throws Exception {
        if (tagId == null){
            throw new Exception("标签类型不能为空");
        }

        Tag tag = tagService.findById(tagId);
        if (tag == null ) {
            throw new Exception("找不到此标签");
        }

        if (tag.getType() != 10 ) {
            throw new Exception("标签类型错误");
        }
        if (recommend == null) {
            throw new Exception("没有推荐内容");
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        recommendTagService.saveCorrelationRecommend(recommend,tagId,enterprise);
        return CommResult.success();
    }




//    @ApiOperation("单个标签下批量保存推荐")
//    @PostMapping("/save")
//    public CommResult<RecommendVo> save(@RequestBody List<Recommend> recommends, Long tagId) throws Exception {
//        if (tagId == null){
//            throw new Exception("标签类型不能为空");
//        }
//
//        Tag tag = tagService.findById(tagId);
//        if (tag == null ) {
//            throw new Exception("找不到此标签");
//        }
//
//        if (tag.getType() != 10 ) {
//            throw new Exception("标签类型错误");
//        }
//        if (recommends == null || recommends.size() <= 0) {
//            throw new Exception("没有推荐存在");
//        }
//        Enterprise enterprise = enterpriseService.getCurrent();
//        recommendTagService.save(recommends,tagId,enterprise);
//        return CommResult.success();
//    }

    @ApiOperation("单个标签下批量删除推荐")
    @PostMapping("/delByTag")
    public CommResult<Recommend> delByTag(Long[] recommends, Long tagId) throws Exception {
        if (tagId == null){
            throw new Exception("标签类型不能为空");
        }

        Tag tag = tagService.findById(tagId);
        if (tag == null ) {
            throw new Exception("找不到此标签");
        }

        if (tag.getType() != 10 ) {
            throw new Exception("标签类型错误");
        }
        if (recommends == null || recommends.length <= 0) {
            throw new Exception("删除的推荐不存在");
        }
        recommendTagService.deleteRecommendTag(recommends,tagId);
        return CommResult.success();
    }



//    @Log(desc = "更新推荐", type = Log.LOG_TYPE.UPDATE)
//    @ApiOperation("单个标签下批量更新推荐")
//    @PostMapping("/update")
//    public CommResult<Recommend> update(@RequestBody List<Recommend> recommends, Long tagId) throws Exception {
//        if (tagId == null){
//            throw new Exception("标签类型不能为空");
//        }
//
//        Tag tag = tagService.findById(tagId);
//        if (tag == null ) {
//            throw new Exception("找不到此标签");
//        }
//
//        if (tag.getType() != 10 ) {
//            throw new Exception("标签类型错误");
//        }
//        if (recommends == null || recommends.size() <= 0) {
//            throw new Exception("没有推荐存在");
//        }
//        recommendTagService.update(recommends,tagId);
//        return CommResult.success();
//    }



//    @ApiOperation("删除推荐标签")
//    @PostMapping("/del/{ids}")
//    public CommResult<Ad> del(@PathVariable String ids) {
//
//        recommendTagService.deleteByIds(ids.split(","));
//        return CommResult.success();
//    }
}
